package cn.schoolwow.quickdao.module.database.ddl.action.flow.index;

import cn.schoolwow.quickdao.domain.QuickDAOConfig;
import cn.schoolwow.quickdao.domain.database.parent.DatabaseType;
import cn.schoolwow.quickdao.module.database.parent.flow.executor.ExecuteUpdateConnectionFlow;
import cn.schoolwow.quickflow.domain.FlowContext;
import cn.schoolwow.quickflow.flow.BusinessFlow;

public class DropIndexFlow implements BusinessFlow {
    @Override
    public void executeBusinessFlow(FlowContext flowContext) throws Exception {
        QuickDAOConfig quickDAOConfig = flowContext.checkInstanceData(QuickDAOConfig.class);
        DatabaseType databaseType = flowContext.checkInstanceData(DatabaseType.class);
        String tableName = (String) flowContext.checkData("tableName");
        String indexName = (String) flowContext.checkData("indexName");

        switch (databaseType){
            case H2:
            case MariaDB:
            case Mysql:{
                flowContext.putTemporaryData("sql", "alter table " + quickDAOConfig.databaseContext.databaseProvider.escape(tableName) + " drop index " + quickDAOConfig.databaseContext.databaseProvider.escape(indexName) + ";");
            }break;
            default:{
                flowContext.putTemporaryData("sql", "drop index " + quickDAOConfig.databaseContext.databaseProvider.escape(indexName) + ";");
            }break;
        }

        flowContext.startFlow(new ExecuteUpdateConnectionFlow())
                .putTemporaryData("name", "删除索引")
                .execute();
    }

    @Override
    public String name() {
        return "删除索引";
    }

}
